//
//  SignedImageView.m
//  HGMM
//
//  Created by @HUI on 15/9/21.
//  Copyright (c) 2015年 cc. All rights reserved.
//

#import "SignedImageView.h"
#import "SignView.h"
#import "NSString+hui.h"
#import "HGPluginUtil.h"

@interface SignedImageView ()
{
    SignView *signView;
    UIView *bgView;
}

@end

@implementation SignedImageView

-(void)showWithBlock:(void (^)(NSString *))block{
    self.signedBlock = block;
    float width = [UIScreen mainScreen].bounds.size.width;
    float height = [UIScreen mainScreen].bounds.size.height;
    self.frame = CGRectMake(0, 0, width, height);
    [self setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.5]];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    bgView = [[UIView alloc]init];
    bgView.bounds = CGRectMake(0, 0, width * 0.8, height * 0.7);
    bgView.center = self.center;
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 5;
    bgView.clipsToBounds = YES;
    [self addSubview:bgView];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, bgView.frame.size.width - 20, 60)];
    lable.numberOfLines = 0;
    NSString *text = [NSString stringWithFormat:@"%@\n%@",self.msg,self.time];
    lable.text = text;
    lable.textColor = [UIColor blueColor];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.font = [UIFont systemFontOfSize:15];
    [lable sizeToFit];
    lable.center = CGPointMake(bgView.frame.size.width/2, lable.frame.size.height/2+10);
    [bgView addSubview:lable];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(2, lable.frame.origin.y + lable.frame.size.height+5, bgView.frame.size.width - 4, 1)];
    view.backgroundColor = [UIColor grayColor];
    [bgView addSubview:view];
    
    signView = [[SignView alloc]initWithFrame:CGRectMake(0, view.frame.origin.y, bgView.frame.size.width, bgView.frame.size.height - view.frame.origin.y)];
    signView.backgroundColor = [UIColor clearColor];
    signView.lineWidth = self.lineWidth;
    [bgView addSubview:signView];
    
    
    NSArray *arr = @[@"取消", @"撤销", @"确定"];
    CGFloat btnWth = bgView.frame.size.width / arr.count;
    for (int i = 0; i < arr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(bgView.frame.origin.x + btnWth * i, bgView.frame.origin.y + bgView.frame.size.height, btnWth, 40);
        button.backgroundColor = [UIColor whiteColor];
        button.tag = i;
        button.layer.borderWidth = 0.5;
        button.layer.borderColor = [UIColor colorWithWhite:0.7 alpha:0.5].CGColor;
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    
}

-(void)btnClick:(UIButton *)button{
    switch (button.tag) {
        case 0:
        {
            [signView clearScreen];
            [self removeFromSuperview];
        }
            break;
        case 1:
        {
            [signView undo];
        }
            break;
        case 2:
        {
            [self saveImage];
            [signView clearScreen];
            [self removeFromSuperview];
        }
            break;
            
        default:
            break;
    }
}

-(void)saveImage{
    UIGraphicsBeginImageContextWithOptions(bgView.frame.size, YES, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [bgView.layer renderInContext:context];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
    NSString *imgName = [NSString stringWithCurrentDate];
    HGPluginUtil *util = [[HGPluginUtil alloc]init];
    NSString *rootPath = [NSString stringWithFormat:@"%@/image",[util getPluginDataDir:self.pluginId]];
    NSError *error = nil;
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:rootPath]) {
        [manager createDirectoryAtPath:rootPath withIntermediateDirectories:YES attributes:nil error:&error];
        if (error) {
            return;
        }
    }
    NSString *imgPath = [NSString stringWithFormat:@"%@.jpg",imgName];
    NSString *savePath = [rootPath stringByAppendingPathComponent:imgPath];
    NSData *imgData = UIImageJPEGRepresentation(newImage, 1);
    [imgData writeToFile:savePath atomically:YES];
    [arr addObject:savePath];
    self.signedBlock(savePath);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


-(void)showx:(NSInteger)x showy:(NSInteger )y WithCallbackBlock:(void (^)(NSString *))block{
    self.signedBlock = block;
    float width = [UIScreen mainScreen].bounds.size.width;
    float height = [UIScreen mainScreen].bounds.size.height;
    self.frame = CGRectMake(0, 0, width, height);
    height = isIphoneX ? (height - 34 - 44 -50) : (height - 20 - 50 );
    //CGFloat yx = isIphoneX ? 44 : 22;
    
    [self setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.5]];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    bgView = [[UIView alloc]init];
    
    CGFloat xp = x > 5 ? 0.5 : (10 - x) * 0.1;
    CGFloat yp = y > 5 ? 0.5 : (10 - x) * 0.1;
    
    //    bgView.bounds = CGRectMake(0, 0, width * xp, height * yp);
    //    bgView.center = self.center;
    CGFloat yx = isIphoneX ?  40 : 20;
    bgView.frame = CGRectMake((HG_SCREEN_WIDTH - width * xp)/2.0, yx + (height - height * yp)/2.0, width * xp, height * yp);
    
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 5;
    bgView.clipsToBounds = YES;
    [self addSubview:bgView];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, bgView.frame.size.width - 20, 60)];
    lable.numberOfLines = 0;
    NSString *text = [NSString stringWithFormat:@"%@\n%@",self.msg,self.time];
    lable.text = text;
    lable.textColor = [UIColor blueColor];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.font = [UIFont systemFontOfSize:15];
    [lable sizeToFit];
    lable.center = CGPointMake(bgView.frame.size.width/2, lable.frame.size.height/2+10);
    [bgView addSubview:lable];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(2, lable.frame.origin.y + lable.frame.size.height+5, bgView.frame.size.width - 4, 1)];
    view.backgroundColor = [UIColor grayColor];
    [bgView addSubview:view];
    
    signView = [[SignView alloc]initWithFrame:CGRectMake(0, view.frame.origin.y, bgView.frame.size.width, bgView.frame.size.height - view.frame.origin.y)];
    signView.backgroundColor = [UIColor clearColor];
    signView.lineWidth = self.lineWidth;
    [bgView addSubview:signView];
    
    
    NSArray *arr = @[@"取消", @"撤销", @"确定"];
    CGFloat btnWth = bgView.frame.size.width / arr.count;
    for (int i = 0; i < arr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(bgView.frame.origin.x + btnWth * i, bgView.frame.origin.y + bgView.frame.size.height, btnWth, 40);
        button.backgroundColor = [UIColor whiteColor];
        button.tag = i;
        button.layer.borderWidth = 0.5;
        button.layer.borderColor = [UIColor colorWithWhite:0.7 alpha:0.5].CGColor;
        [button setTitle:arr[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    
}

@end
